package libra.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import libra.model.JsonResult;
import libra.model.Provider_Pay;

@RestController
@RequestMapping("consumer")
public class ConsumerReqController {
	@Autowired
	ConsumerFeignService feignService;
	
	@Value("${server.servlet.context-path}")
	String from;
	
	@RequestMapping("create")
	public Object create( HttpServletRequest req ){
		return feignService.create(from + "_@_" + Provider_Pay.genUuid());
	}
	
	@RequestMapping("find")
	public Object find( Integer id, HttpServletRequest req ){
		return feignService.find(id);
	}
	@RequestMapping("list")
	public Object list( HttpServletRequest req ){
		return feignService.list();
	}
	
	@RequestMapping("ok")
	public Object Ok( HttpServletRequest req ){
		return feignService.Ok();
	}
	@RequestMapping("timeOutWithHystrix")
	public Object TimeOutWithHystrix( Boolean flag, HttpServletRequest req ){
		return feignService.TimeOutWithHystrix(flag);
	}
	@RequestMapping("errorWithHystrix")
	public Object ErrorWithHystrix( Boolean flag, HttpServletRequest req ){
		return feignService.ErrorWithHystrix( flag );
	}
	
	@RequestMapping("timeOut")
	public Object TimeOut( Boolean flag, HttpServletRequest req ) throws Exception{
		return feignService.TimeOut(flag);
	}
	@RequestMapping("error")
	public Object Error( Boolean flag, HttpServletRequest req ) throws Exception{
		return feignService.Error( flag );
	}
	
	
	@Autowired
	DiscoveryClient discoveryClient;
	@RequestMapping("DiscoveryClient")
	public Object discoveryClient( HttpServletRequest req ){
		Map<String, Object> result = new HashMap<String, Object>();
		
		// 获取EurekaServer的注册服务列表
		List<String> services = discoveryClient.getServices();
		System.out.println( "discoveryClient.getServices() -> "+services );
		for (String serviceName : services) {
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			result.put(serviceName, list);
			// 通过注册为服务名称，spring.application.name来进行服务的检索发现
			List<ServiceInstance> srvList = discoveryClient.getInstances(serviceName);
			for( ServiceInstance si : srvList ) {
				Map<String, Object> instance = new HashMap<String, Object>();
				list.add(instance);
				
				instance.put("getServiceId", si.getServiceId());
				instance.put("getHost", si.getHost());
				instance.put("getPort", si.getPort());
				instance.put("getUri", si.getUri());
				instance.put("getInstanceId", si.getInstanceId());
				instance.put("getScheme", si.getScheme());
				instance.put("getMetadata", si.getMetadata());
				System.out.println(instance);
			}
		}
		
		return result;
	}
}
